package com.zhuoyue.biz.core.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.dao.TblDeviceTaskMapper;
import com.zhuoyue.biz.core.api.domain.TblDeviceTask;
import com.zhuoyue.biz.core.api.dto.rep.TimedTaskReqDTO;
import com.zhuoyue.biz.core.api.service.DeviceTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeviceTaskServiceImpl implements DeviceTaskService {
    private final TblDeviceTaskMapper deviceTaskMapper;

    private final RedisTemplate redisTemplate;

    /**
     * 删除定时任务
     */
    private static final String DELETE_TIMED_TASK = "DeleteTimedTask";
    /**
     * 控制类型
     */
    private static final String CONTROL_TYPE = "controlType";

    @Override
    public int addOrUpdateDeviceTask(String zigbeeMac, int taskIndex) {
        String key = "set_device_task_zigbeeMac_" + zigbeeMac + "_index_" + taskIndex;
        Object object = redisTemplate.opsForValue().get(key);
        if (object == null) {
            return 0;
        }
        redisTemplate.delete(key);
        int count = 0;
        Map<String, Object> information = (Map<String, Object>) object;
//        若是删除任务
        if (DELETE_TIMED_TASK.equals(information.get(CONTROL_TYPE).toString())) {
            count = deleteDeviceTask(zigbeeMac, taskIndex);
        } else {
            //添加或修改任务
            TblDeviceTask deviceTask = (TblDeviceTask) information.get("deviceTask");

            //从数据库中查询ID
            if (deviceTask.getId() == null) {
                deviceTask.setId(getIdByZigbeeMacAndIndex(deviceTask.getZigbeeMac(), deviceTask.getTaskIndex()));
            }

            //若ID还是null，则说数据库中没有对应的数据，需要插入，否在是修改
            if (deviceTask.getId() == null) {
                count = deviceTaskMapper.insert(deviceTask);
            } else {
                count = deviceTaskMapper.updateById(deviceTask);
            }
        }

        return count;
    }


    @Override
    public String getIdByZigbeeMacAndIndex(String zigbeeMac, int taskIndex) {
        TblDeviceTask deviceTask = deviceTaskMapper.selectOne(Wrappers.<TblDeviceTask>lambdaQuery().eq(TblDeviceTask::getZigbeeMac, zigbeeMac).eq(TblDeviceTask::getTaskIndex, taskIndex));
        if (deviceTask == null) {
            return null;
        }

        return deviceTask.getId();
    }

    @Override
    public List<TimedTaskReqDTO> getTasksByZigbeeMac(String zigbeeMac) {
        List<TblDeviceTask> tblDeviceTasks = deviceTaskMapper.selectList(Wrappers.<TblDeviceTask>lambdaQuery().eq(TblDeviceTask::getZigbeeMac, zigbeeMac).orderByAsc(TblDeviceTask::getTaskIndex));
        if (CollectionUtils.isEmpty(tblDeviceTasks)) {
            return null;
        }

        //将DO转为DTO
        List<TimedTaskReqDTO> timedTaskDtos = new ArrayList<>();
        for (TblDeviceTask tblDeviceTask : tblDeviceTasks) {
            timedTaskDtos.add(new TimedTaskReqDTO().dOtoDto(tblDeviceTask));
        }
        return timedTaskDtos;
    }

    @Override
    public int deleteDeviceTask(String zigbeeMac, int taskIndex) {
        return deviceTaskMapper.delete(Wrappers.<TblDeviceTask>lambdaQuery().eq(TblDeviceTask::getZigbeeMac, zigbeeMac).eq(TblDeviceTask::getTaskIndex, taskIndex));
    }

    /**
     * 删除所有信息
     * @param zigbeeMac
     * @return
     */
    @Override
    public int deleteAll(String zigbeeMac) {
        int delete = deviceTaskMapper.delete(Wrappers.<TblDeviceTask>lambdaQuery()
                .eq(TblDeviceTask::getZigbeeMac, zigbeeMac));
        return delete;
    }
}
